package lee.vioson.ebook.ui.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import coil.compose.rememberImagePainter
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.ui.Constants
import lee.vioson.ebook.ui.navigate
import lee.vioson.remotedata.bean.Book

/**
 *Author:viosonlee
 *Date:2023/6/3
 *DESCRIPTION:
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BookCard(
    book: Book,
    navHostController: NavHostController,
    editable: Boolean = false,
    initEditMode: Boolean = false,
    initSelected: Boolean = false,
    onEditModeChange: ((Boolean) -> Unit)? = null,
    onSelectChange: ((Boolean) -> Unit)? = null
) {
    var editMode by remember { mutableStateOf(initEditMode) }
    var selected by remember { mutableStateOf(initSelected) }
    LaunchedEffect(key1 = initSelected, block = {
        selected = initSelected
    })
    LaunchedEffect(key1 = selected, block = {
        onSelectChange?.invoke(selected)
    })
    LaunchedEffect(key1 = initEditMode, block = {
        editMode = initEditMode
    })
    LaunchedEffect(key1 = editMode, block = {
        onEditModeChange?.invoke(editMode)
    })
    Box(
        modifier = Modifier
            .height(160.dp)
            .width(110.dp)
            .combinedClickable(
                onClick = {
                    //to detail
                    navHostController.navigate(
                        AppDestinations.BOOK_DETAIL_ROUTE,
                        mapOf(Constants.bookId to book.id)
                    )
                },
                onLongClick = {
                    if (editable) {
                        //change to edit mode
                        editMode = !editMode
                        selected = editMode
                    }
                })
    ) {
        AsyncImage(
            modifier = Modifier
                .fillMaxSize()
                .clip(RoundedCornerShape(4.dp)),
            model = book.coverPhoto,
            contentDescription = "thumb pic",
            contentScale = ContentScale.Crop
        )
        Column {
            Row {
//                book.marks.forEach {
//                    BookMark(
//                        modifier = Modifier,
//                        content = it,
//                        color = Color.Red
//                    )
//                    Spacer(modifier = Modifier.width(4.dp))
//                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Row(
                Modifier
                    .background(Color.Black.copy(alpha = 0.2f))
                    .padding(vertical = 4.dp, horizontal = 8.dp),
                verticalAlignment = Alignment.Bottom
            ) {
                Column(Modifier.weight(1f)) {
                    Text(
                        text = book.bookName,
                        fontSize = 14.sp,
                        color = AppTheme.colors.onBackground,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
//                    Text(
//                        text = book.typeString,
//                        fontSize = 12.sp,
//                        color = AppTheme.colors.onBackground,
//                        maxLines = 1, overflow = TextOverflow.Ellipsis
//                    )
                }
                BookMark(
                    modifier = Modifier,
                    content = book.recommendIndex.toString(),
                    color = Color.Red
                )
            }
        }
        if (editMode) {
            Checkbox(checked = selected, onCheckedChange = { selected = it })
        }
    }
}


@Composable
fun BookMark(modifier: Modifier = Modifier, content: String, color: Color) {
    Box(
        modifier = Modifier
            .then(modifier)
            .clip(RoundedCornerShape(2.dp))
            .background(color)
            .padding(horizontal = 3.dp, vertical = 2.dp),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = content,
            style = TextStyle(fontSize = 6.sp, color = AppTheme.colors.onBackground)
        )
    }
}